<?php
$pageTitle = "教师编辑";
require 'header.php';

// 获取教师ID
$teacher_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// 查询教师信息
$stmt = $pdo->prepare('SELECT * FROM teachers WHERE id = ?');
$stmt->execute([$teacher_id]);
$teacher = $stmt->fetch();

if (!$teacher) {
    die('教师信息不存在');
}
?>

<div class="container mt-5">
    <h2>编辑教师信息</h2>
    <form action="teacher_edit_api.php" method="post">
        <input type="hidden" name="id" value="<?php echo $teacher['id']; ?>">
        
        <div class="mb-3">
            <label for="name" class="form-label">姓名</label>
            <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($teacher['name']); ?>" required>
        </div>
        
        <div class="mb-3">
            <label for="sex" class="form-label">性别</label>
            <select class="form-select" id="sex" name="sex" required>
                <option value="男" <?php echo ($teacher['sex'] === '男') ? 'selected' : ''; ?>>男</option>
                <option value="女" <?php echo ($teacher['sex'] === '女') ? 'selected' : ''; ?>>女</option>
            </select>
        </div>
        
        <div class="mb-3">
            <label for="birthday" class="form-label">出生日期</label>
            <input type="text" class="form-control" id="birthday" name="birthday" value="<?php echo $teacher['birthday']; ?>" required>
        </div>
        
        <div class="mb-3">
            <label for="work_time" class="form-label">参加工作时间</label>
            <input type="text" class="form-control" id="work_time" name="work_time" value="<?php echo htmlspecialchars($teacher['work_time']); ?>" required>
        </div>
        
        <div class="mb-3">
            <label for="here_work_time" class="form-label">入职山阳区工作时间</label>
            <input type="text" class="form-control" id="here_work_time" name="here_work_time" value="<?php echo $teacher['here_work_time'] ? htmlspecialchars($teacher['here_work_time']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="subject" class="form-label">任教学科</label>
            <input type="text" class="form-control" id="subject" name="subject" value="<?php echo htmlspecialchars($teacher['subject']); ?>" required>
        </div>
        
        <div class="mb-3">
            <label for="grade" class="form-label">年级</label>
            <input type="text" class="form-control" id="grade" name="grade" value="<?php echo $teacher['grade'] ? htmlspecialchars($teacher['grade']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="major" class="form-label">专业</label>
            <input type="text" class="form-control" id="major" name="major" value="<?php echo $teacher['major'] ? htmlspecialchars($teacher['major']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="educational" class="form-label">学历</label>
            <input type="text" class="form-control" id="educational" name="educational" value="<?php echo $teacher['educational'] ? htmlspecialchars($teacher['educational']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="qualifications" class="form-label">职称</label>
            <input type="text" class="form-control" id="qualifications" name="qualifications" value="<?php echo $teacher['qualifications'] ? htmlspecialchars($teacher['qualifications']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="qualifications_time" class="form-label">职称证取得时间</label>
            <input type="text" class="form-control" id="qualifications_time" name="qualifications_time" value="<?php echo $teacher['qualifications_time'] ? htmlspecialchars($teacher['qualifications_time']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="master_level" class="form-label">名师骨干层次</label>
            <input type="text" class="form-control" id="master_level" name="master_level" value="<?php echo $teacher['master_level'] ? htmlspecialchars($teacher['master_level']) : ''; ?>">
        </div>
        
        <div class="mb-3">
            <label for="school" class="form-label">单位</label>
            <select class="form-select" id="school" name="school" required>
                <?php
                $sql = "SELECT school FROM users GROUP BY school";
                $stmt = $pdo->query($sql);
                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
                        <option value="<?php echo $row['school']?>" <?php echo ($teacher['school'] === $row['school']) ? 'selected' : ''; ?>><?php echo $row['school']?></option>
                <?php } ?>
            </select>
        </div>
        
        <div class="mb-3">
            <label for="phone" class="form-label">联系电话</label>
            <input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($teacher['phone']); ?>" required>
        </div>
        
        <button type="submit" class="btn btn-primary">保存修改</button>
        <a href="teachers.php" class="btn btn-secondary">取消</a>
    </form>
</div>

<?php require 'footer.php'; ?>